package com.tm;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.tm.controller.InstrumentController;
import com.tm.pojo.Instrument;

@SuppressWarnings("serial")
public class StoreInstrument extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//do nothing
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			UserService userService = UserServiceFactory.getUserService();
			if (userService.isUserLoggedIn() && userService.getCurrentUser().getNickname().equals("vahe@andonians.com")) {
				Instrument i = new Instrument();
				i.setStrTerm(req.getParameter("term"));
				i.setName(req.getParameter("name"));
				i.setType(req.getParameter("type"));
				i.setDefinition(req.getParameter("definition"));
				i.setBloombergId(req.getParameter("bloombergId"));
				i.setCurrency(req.getParameter("currency"));
				i.setDaycountConvention(req.getParameter("daycountConvention"));
				i.setCompoundingFrequency(req.getParameter("compoundingFrequency"));
				InstrumentController.getInstance().saveNewInstrument(i);
				PrintWriter out = resp.getWriter();
				out.println(i.toJSONObject());
			}
		} catch (Throwable e) {
			PrintWriter out = resp.getWriter();
			out.println("Error: " + e.getMessage());
		}
	}
}
